package com.yunfeng.aolin.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.yunfeng.common.log.Log;
import com.yunfeng.common.log.LogMessageObject;
import com.yunfeng.common.log.impl.LogUitl;
import com.yunfeng.common.util.FileUtils;
import com.yunfeng.common.util.AjaxObject;
import com.yunfeng.common.util.Page;
import com.yunfeng.aolin.entity.SendMaterials;
import com.yunfeng.aolin.service.SendMaterialsService;

@Controller
@RequestMapping("/management/materials/sendmaterials")
public class SendMaterialsController {

	@Autowired
	private SendMaterialsService sendMaterialsService;
	
	private static final String CREATE = "management/materials/sendmaterials/create";
	private static final String UPDATE = "management/materials/sendmaterials/update";
	private static final String LIST = "management/materials/sendmaterials/list";
	private static final String VIEW = "management/materials/sendmaterials/view";
	
	@InitBinder
	public void dataBinder(WebDataBinder dataBinder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		dataBinder.registerCustomEditor(Date.class, 
				new CustomDateEditor(df, true));
	}
	
	@RequiresPermissions("SendMaterials:save")
	@RequestMapping(value="/create", method=RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
		return CREATE;
	}
	
	@Log(message="添加了id={0}发料记录。")
	@RequiresPermissions("SendMaterials:save")
	@RequestMapping(value="/create", method=RequestMethod.POST)
	public @ResponseBody String create(@Valid SendMaterials sendMaterials) {
		sendMaterialsService.saveOrUpdate(sendMaterials);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{sendMaterials.getId()}));
		return AjaxObject.newOk("发料记录添加成功！").toString();
	}
	
	@ModelAttribute("preloadSendMaterials")
	public SendMaterials preload(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			SendMaterials sendMaterials = sendMaterialsService.get(id);
			return sendMaterials;
		}
		return null;
	}
	
	@RequiresPermissions("SendMaterials:edit")
	@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	public String preUpdate(@PathVariable Long id, Map<String, Object> map) {
		SendMaterials sendMaterials = sendMaterialsService.get(id);
		map.put("sendMaterials", sendMaterials);
		return UPDATE;
	}
	
	@Log(message="修改了id={0}发料记录的信息。")
	@RequiresPermissions("SendMaterials:edit")
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody String update(@Valid @ModelAttribute("preloadSendMaterials")SendMaterials sendMaterials) {
		sendMaterialsService.saveOrUpdate(sendMaterials);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{sendMaterials.getId()}));
		return AjaxObject.newOk("发料记录修改成功！").toString();
	}

	@Log(message="删除了id={0}发料记录。")
	@RequiresPermissions("SendMaterials:delete")
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	public @ResponseBody String delete(@PathVariable Long id) {
		sendMaterialsService.delete(id);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{id}));
		return AjaxObject.newOk("发料记录删除成功！").setCallbackType("").toString();
	}
	
	@Log(message="批量删除了id={0}发料记录。")
	@RequiresPermissions("SendMaterials:delete")
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody String deleteMany(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			SendMaterials sendMaterials = sendMaterialsService.get(ids[i]);
			sendMaterialsService.delete(sendMaterials.getId());
		}
		
		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{Arrays.toString(ids)}));
		return AjaxObject.newOk("发料记录删除成功！").setCallbackType("").toString();
	}

	@RequiresPermissions("SendMaterials:view")
	@RequestMapping(value="/list", method={RequestMethod.GET, RequestMethod.POST})
	public String list(Page page, String keywords, Map<String, Object> map) {
		List<SendMaterials> sendMaterialss = null;
		if (StringUtils.isNotBlank(keywords)) {
			sendMaterialss = sendMaterialsService.findByLiaohao(page, keywords);
		} else {
			sendMaterialss = sendMaterialsService.findAll(page);
		}

		map.put("page", page);
		map.put("sendMaterialss", sendMaterialss);
		map.put("keywords", keywords);

		return LIST;
	}
	
	@RequiresPermissions("SendMaterials:view")
	@RequestMapping(value="/view/{id}", method={RequestMethod.GET})
	public String view(@PathVariable Long id, Map<String, Object> map) {
		SendMaterials sendMaterials = sendMaterialsService.get(id);
		map.put("sendMaterials", sendMaterials);
		return VIEW;
	}
	
	
	@RequiresPermissions("SendMaterials:exportExcel")
	@RequestMapping(value = "/exportExcel", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void exportExcel(HttpServletResponse response) throws Exception {
		List<SendMaterials> sendMaterialss = sendMaterialsService.findAll();
		FileUtils.exportExcel(response, sendMaterialss);
	}

}
